<template>

  <div>

    <h1 style="color: grey">当前位置 >> 学报论文库 >> 学报检索</h1>

    <div class="box" style="display: inline-block">
      <div class="searchBox" style="display: flex">

        <div style="margin: 10px 0;display: flex">
          <el-input v-model="search" placeholder="输入关键字查询" style="width: 400px;" @clear="load" @keyup.enter="load"
                    clearable></el-input>
          <el-button style="margin-left: 10px" type="primary" @click="load" circle>
            <el-icon>
              <search/>
            </el-icon>
          </el-button>
        </div>
        <div style="margin: 10px 30px">
          <el-select v-model="sort" placeholder="请选择搜索类型" style="margin-left: 20px;width: 200px" @change="load">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
    </div>

    <div class="block" style="margin-top: 30px" v-loading="loadering">
      <!--      表格-->
      <el-table :data="information" style="width: 98%" border>
        <el-table-column fixed prop="name" label="学报名称"  />
        <el-table-column prop="magazine" label="刊物名称"  />
        <el-table-column prop="category" label="刊物类别"  />
        <el-table-column prop="level" label="刊物级别"  />
        <el-table-column prop="sponsor" label="主办单位"  />
        <el-table-column prop="author" label="作者"  />
        <el-table-column prop="publishTime" label="发表时间"  />
        <el-table-column prop="issn" label="刊号"/>
        <el-table-column fixed="right" label="操作" width="200px">
          <template #default="scope">
            <el-row>
              <el-col :span="6">
                <el-button size="small" type="success" @click="handleFind(scope.row)">查看</el-button>
              </el-col>
              <el-col :span="6" style="margin-left: 10px">
                <el-button size="small" type="primary" @click="handleEdit(scope.row)">修改</el-button>
              </el-col>
              <el-col :span="6" style="margin-left: 10px">
                <el-popconfirm title="确认删除吗?" @confirm="handleDrop(scope.row)">
                  <template #reference>
                    <el-button
                        size="small"
                        type="danger">删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          class="mt-4"
          background
          style="margin-top: 10px"
      >
      </el-pagination>
    </div>


    <!--
    抽屉2,详情框
    close-on-click-modal:是否可以点击抽屉外关闭抽屉弹窗
    -->
    <el-drawer
        v-model="drawer2"
        title="详情"
        :close-on-click-modal=false
    >
      <el-scrollbar height="100%">
        <el-form :model="form" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm" :rules="formRules"
                 style="margin-right: 20px;"
        >
          <el-form-item label="学报名称" prop="name">
            <el-input type="text" v-model="form.name" autocomplete="off" @blur.prevent="changeName()"></el-input>
            <span v-if="this.torf === false" style="font-size: 10px;color: red">该论文已录入</span>
          </el-form-item>
          <el-form-item label="刊物名称" prop="magazine">
            <el-input type="text" v-model="form.magazine" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="刊物类别" prop="category">
            <el-input type="text" v-model="form.category" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="刊物级别" prop="level">
            <el-input type="text" v-model="form.level" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="主办单位" prop="sponsor">
            <el-input type="text" v-model="form.sponsor" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="作者" prop="author">
            <el-input type="text" v-model="form.author" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="职工号" prop="pnoo">
            <el-input type="text" v-model="form.pnoo" autocomplete="off" @blur.prevent="changepNo()"></el-input>
            <span v-if="this.torf1 === false" style="font-size: 10px;color: red">数据库中没有找到该职工</span>
          </el-form-item>

          <el-form-item label="发表时间" prop="publishTime">

            <el-date-picker v-model="form.publishTime"
                            type="date" placeholder="请选择发表时间" style="width: 100%;">
            </el-date-picker>

          </el-form-item>

          <el-form-item label="刊号" prop="issn">
            <el-input type="text" v-model="form.issn" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="文件" prop="files">
            <el-upload
                class="upload-demo"
                drag
                action="http://222.21.119.252:9999/files/upload"
                multiple
                list-type="text"
                :on-success="filesUploadSuccess"
                :on-error="filesUploadError"
                :before-upload="checkFileBefore"
                :data=fileVO
                :limit="1"
                :on-exceed='limitCheck'
                :file-list="fileList"
            >
              <el-icon class="el-icon--upload">
                <upload-filled/>
              </el-icon>
              <div class="el-upload__text">
                拖拽上传 或 <em>点击上传</em>
              </div>
              <template #tip>
                <div class="el-upload__tip">
                  仅支持上传一个文件,且最大容量为100MB
                </div>
              </template>
            </el-upload>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"
                      placeholder="请输入内容" v-model="form.remark" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="editForm"
                       style="width:100%; height:100%;margin-bottom:15px;">提交
            </el-button>
          </el-form-item>
        </el-form>
      </el-scrollbar>
    </el-drawer>

    <el-drawer
        v-model="findDrower"
        title="详情"

    >
      <el-descriptions :column="1" border>
        <el-descriptions-item
            label="id"
            label-align="right"
            align="center">
          {{ this.findFrom.id }}
        </el-descriptions-item>

        <el-descriptions-item label="学报名称" label-align="right" align="center"> {{ this.findFrom.name }}
        </el-descriptions-item>

        <el-descriptions-item label="刊物名称" label-align="right" align="center"> {{ this.findFrom.magazine }}
        </el-descriptions-item>

        <el-descriptions-item label="刊物类别" label-align="right" align="center">
          {{ this.findFrom.category }}
        </el-descriptions-item>

        <el-descriptions-item label="刊物级别" label-align="right" align="center">
          {{ this.findFrom.level }}
        </el-descriptions-item>


        <el-descriptions-item label="主办单位" label-align="right" align="center">
          {{ this.findFrom.sponsor }}
        </el-descriptions-item>

        <el-descriptions-item label="作者" label-align="right" align="center">
          {{ this.findFrom.author }}
        </el-descriptions-item>

        <el-descriptions-item label="发表时间" label-align="right" align="center">
          {{ this.findFrom.publishTime }}
        </el-descriptions-item>

        <el-descriptions-item label="刊号" label-align="right" align="center">
          {{ this.findFrom.issn }}
        </el-descriptions-item>

        <el-descriptions-item label="文件资料" label-align="right" align="center">
          <el-popconfirm
              title="确认下载吗?"
              @confirm="downthis1(this.findFrom.files)"
          >
            <template #reference>
              <el-button type="success" size="small">下载文件</el-button>
            </template>
          </el-popconfirm>
        </el-descriptions-item>

        <el-descriptions-item label="职工号" label-align="right" align="center" >
          {{ this.findFrom.pnoo }}
        </el-descriptions-item>

        <el-descriptions-item label="备注" label-align="right" align="center" >
          {{ this.findFrom.remark }}
        </el-descriptions-item>

      </el-descriptions>
    </el-drawer>
  </div>
</template>
<script>
import request from "@/utils/request";
import {Search, Edit, Delete, UploadFilled} from '@element-plus/icons-vue'

export default {
  name: "SearchLunWen",
  components: {
    Search,
    Edit,
    Delete,
    UploadFilled
  },
  data() {
    return {
      fileList: [{
        name: '',
        value: ''
      }],
      fileVO: {
        sort: 'newspaper',
        fileName: ''
      },
      torf: true,
      torf1: true,
      formRules: {
        name: [
          {required: true, message: '请输入项目名称', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请输入项目作者', trigger: 'blur'}
        ],
        category: [
          {required: true, message: '请输入刊物类别', trigger: 'blur'}
        ],
        files: [
          {required: true, message: '请上传文件资料', trigger: 'blur'}
        ],
        issn: [
          {required: true, message: '请输入项目刊号', trigger: 'blur'}
        ],
        level: [
          {required: true, message: '请输入刊物级别', trigger: 'blur'}
        ],
        magazine: [
          {required: true, message: '请输入刊物名称', trigger: 'blur'}
        ],
        pnoo: [
          {required: true, message: '请输入职工号', trigger: 'blur'}
        ],
        publishTime: [
          {required: true, message: '请选择发表时间', trigger: 'blur'}
        ],
        sponsor: [
          {required: true, message: '请输入主办单位', trigger: 'blur'}
        ]
      },
      drawer2: false,
       findDrower: false,
      loadering: true,
      form: {},
      options: [
        {
          value: 'Author',
          label: '作者'
        },
        {
          value: 'Name',
          label: '学报名称'
        },
        {
          value: 'Level',
          label: '刊物级别'
        },
        {
          value: 'ISSN',
          label: '期刊号'
        },
        {
          value: 'Magazine',
          label: '刊物名称'
        },
        {
          value: 'Category',
          label: '刊物类型'
        }
      ],
      //数据
      information: [],
      currentPage: 1,
      total: 0,
      search: '',
      pageSize: 10,
      sort: '',
      pname: ''
    };
  },

  methods: {
    handleFind(row) {
      //深拷贝
      this.findFrom = JSON.parse(JSON.stringify(row));

      this.findDrower = true;
    },
    //修改
    editForm() {
      this.$refs.ruleForm.validate(valid => {
        if (!valid) {
          this.$message.error("请输入必选项!")
          return
        }

        request.put("/newspaper", this.form).then(res => {
          if (res.code === '200') {
            //弹出成功框
            this.$message.success("修改成功")
            this.load();
            this.drawer2 = false;
          } else {
            this.$message.error("修改失败!请确保表单各项填写完整正确!");
          }
        }).catch(() => {   //防止未知bug,执行不了Controller方法直接报错
          this.$message.error("服务器错误!请联系管理员或重新尝试!");
        })


      })
    },

    downthis1(file) {
      //下载文件
      window.location.href = file;
    },

    handleEdit(row) {
      //深拷贝
      this.form = JSON.parse(JSON.stringify(row));
      //显示文件列表
      this.fileList[0].name = '原文件';
      this.fileList[0].uri = this.form.files;

      //用来判断 name 是否重复
      this.pname = this.form.name;
      //打开右边的抽屉
      this.drawer2 = true;
    },

    //删除
    handleDrop(row) {
      //根据 主键 name 删除
      request.delete("/files/" + this.fileVO.sort + "/" + row.name).then(() => {
        //无论文件是否存在都执行下一步,存在则删除,不存在则不理会,然后删除newspaper
        //删除 newspaper 的 res
        request.delete("/newspaper/" + row.id).then(res => {
          if (res.code === '200') {
            //弹出成功框
            this.$message.success("删除成功")
            this.load();
          }
        }).catch(() => {   //防止未知bug,执行不了Controller方法直接报错
              this.$message.error("服务器错误!请联系管理员或重新尝试!");
            }
        );
      }).catch(() => {
        this.$message.error("服务器错误!请联系管理员或重新尝试!");
      });
    },

    //加载
    load() {
      request.get(`/newspaper`, {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search,
          sort: this.sort
        }
      }).then(res => {
        if (res.code === '200') {
          this.information = res.data.records;
          this.total = res.data.total;
          this.loadering = false;
        }
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
    changeName() {
      if (this.form.name === '') {
        this.torf = true;
      } else {
        request.get(`/newspaper/name/` + this.form.name).then(res => {
          if (res.code === '200') {
            //表示不重复,可以通过
            this.torf = true;
          } else { //表示有这个主键,再判断是不是未修改的那个名字
            if (res.data.name === this.pname) {
              //表示未修改学报名称,可以通过
              this.torf = true;
            } else {
              //重复,不能通过
              this.torf = false;
            }

          }
        })
      }
    },
    changepNo() {
      if (this.form.pnoo === '') {
        this.torf1 = true;
      } else {
        request.get(`/sperson/pno/` + this.form.pnoo).then(res => {
          if (res.code === '200') {
            //表示没有,不能通过
            this.torf1 = false;
          } else {
            //表示有,则可以通过
            this.torf1 = true;
          }
        });
      }
    },

    //文件上传成功调用
    filesUploadSuccess(res) {
      this.form.files = res.data;
    },

    //上传之前
    checkFileBefore(file) {
      if (this.form.name === '') {
        this.$message.error("上传文件前请先输入学报名称!");
        return false;
      }
      //判断文件大小
      if (file.size / 1024 / 1024 > 100) {
        this.$message.error("文件大小不得超过100MB");
        return false;
      }
      //文件上传的参数之一
      this.fileVO.fileName = this.form.name;
    },
  //超出文件数量限制
    limitCheck() {
      this.$message.warning('只能上传一个文件');
    },
    //上传文件失败提示
    filesUploadError() {
      this.$message.error("上传失败,请重新尝试或联系管理员");
    }
  },
  created() {
    this.load();
  },


}
</script>

<style scoped>
.box {
  margin: 0 auto;
  padding-top: 20px;
  height: 50px;
  width: 100%;
}

.searchBox {
  margin: 0 auto;
  width: 60%;
  position: relative;
}
/deep/ .el-upload .el-upload-dragger{
  width: 300px;
}



</style>
